package com.liyujie.networkrequest.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dyhdyh.support.countdowntimer.CountDownTimerSupport;
import com.dyhdyh.support.countdowntimer.OnCountDownTimerListener;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.listener.BeCommonCallback;
import com.lxj.xpopup.core.CenterPopupView;
import com.sjl.scanner.UsbKeyboardAutoScan;
import com.sjl.scanner.listener.OnScanListener;

public class ScanQRCodesPopup extends CenterPopupView {

    /**
     * usb键盘模式扫码示例，无需EditText接收,推荐
     */
    private UsbKeyboardAutoScan usbKeyboardAutoScan;


    /**
     * 倒计时
     */
    private CountDownTimerSupport mCountDownTimerSupport = new CountDownTimerSupport(10000,1000);

    public ScanQRCodesPopup(@NonNull Context context) {
        super(context);

    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_scan_qr_codes;

    }


    @Override
    protected void onCreate() {
        super.onCreate();
        TextView Content = findViewById(R.id.Content);
        Content.setText("请扫码");


        usbKeyboardAutoScan = new UsbKeyboardAutoScan();
        usbKeyboardAutoScan.setOnScanListener(new OnScanListener() {
            @Override
            public void onScanSuccess(String barcode) {
                if(!TextUtils.isEmpty(barcode)){
                    if(null != beCommonCallback){
                        beCommonCallback.onBeCommonCallback(barcode);
                    }
                    try {
                        dismiss();
                    }catch (Exception e){
                        e.getMessage();
                    }
                }
            }
        });

        TextView Countdown = findViewById(R.id.Countdown);
        //倒计时监听
        mCountDownTimerSupport.setOnCountDownTimerListener(new OnCountDownTimerListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                Countdown.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                try {
                    dismiss();
                }catch (Exception e){
                    e.getMessage();
                }
            }
        });
        /**
         * 重置时间
         */
        ResetTime();

    }

    /**
     * 重置时间
     */
    protected void ResetTime(){
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport.start();
        }
    }

    /**
     * 重置暂停
     */
    protected void ResetPause(){
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport.pause();
        }
    }




    private BeCommonCallback beCommonCallback;
    public void setBeCommonCallback(BeCommonCallback beCommonCallback) {
        this.beCommonCallback = beCommonCallback;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){//退格键
            return super.dispatchKeyEvent(event);
        }else if(event.getKeyCode() == KeyEvent.KEYCODE_FORWARD_DEL){//	删除键
            return super.dispatchKeyEvent(event);
        }else if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){//返回键
            return super.dispatchKeyEvent(event);
        }else if(usbKeyboardAutoScan.isIntercept()){
            usbKeyboardAutoScan.analysisKeyEvent(event);
            return false;
        }else {
            return super.dispatchKeyEvent(event);
        }
    }


    @Override
    public void dismiss() {
        if(null != mCountDownTimerSupport){
            mCountDownTimerSupport.reset();
            mCountDownTimerSupport = null;
        }
        super.dismiss();
    }
}
